<?php
class Borrower_model extends General_db_model
{
	private $tbl_borrower = 'tbl_borrowers';
	function total_borrower()
	{
                $total = $this->db->count_all_results($this->tbl_borrower);
		return $total;
	}

	function get_borrower($limit=NULL, $start=NULL)
	{
		$query = "SELECT * from tbl_borrowers;";
                $result = $this->db->query($query);
                $finalResult = $result->result();
                return $finalResult;
    }
		
	function get_publication_list($borrower_id=NULL){
			$query = "select tbq.*,DATEDIFF(tbq.return_date, now()) as date_diff,tpi.coding,tpi.publisher,tpi.title from tbl_borrower_quantities as tbq
					join tbl_publication_info as tpi on tpi.id=tbq.publication_id
					and tbq.borrower_id=?";
			$result = $this->db->query($query,$borrower_id);
			$finalResult = $result->result();
		   // echo "<pre>";print_r($finalResult);exit;
			return $finalResult;
	}
	
	function update_quantity($publication_id, $type, $qty, $refund=NULL)
	{
		$current = $this->current_status($publication_id, $type);
		
		if($refund)
			$update['total_qty_borrow'] = $current->total_qty_borrow + $qty;
			
		$update['total_qty_distribution'] = $current->total_qty_distribution - $qty;
		
		/*if($current->total_qty_distribution >= $qty)
		{
			$update['total_qty_distribution'] = $current->total_qty_distribution - $qty;
			$update['total_qty_store'] = $current->total_qty_store;
		}
		else
		{
			$update['total_qty_distribution'] = 0;
			$update['total_qty_store'] = $current->total_qty_store - $qty + $current->total_qty_distribution;
		}*/
		
		$update['total_qty'] = $update['total_qty_distribution'] + $current->total_qty_store + $current->total_qty_box;
		
		$this->general_db_model->update('tbl_publication_quantity', $update, 'publication_id = '.$publication_id.' and type = "'.$type.'"');
		
	}
	
	function current_status($publication_id, $type)
	{
		$this->db->where('publication_id', $publication_id);
		$this->db->where('type', $type);
		$query = $this->db->get('tbl_publication_quantity');
		return $query->row();
	}
	
	function get_notifications_to_s()
	{
		$sql = 'select a.*,b.id as borrower_qty_id from tbl_borrowers as a join tbl_borrower_quantities as b on (a.id = b.borrower_id) where curdate() > b.return_date and b.status = "0"';
		$query = $this->db->query($sql);
		return $query->result();
	}
        function get_borrowers_by_date($date)
	{
		$sql = 'select a.*, b.publication_id, b.type, b.refundable_qty, b.non_refundable_qty, b.issue_date, b.return_date, b.status, c.title from tbl_borrowers as a join tbl_borrower_quantities as b on (a.id = b.borrower_id) join tbl_publication_info as c on(b.publication_id = c.id) where year(b.issue_date) = year("'.$date.'") and month(b.issue_date) = month("'.$date.'")';
		$query = $this->db->query($sql);
		return $query->result();
	}
        function get_borrower_notifications_to_send_chitthi()
	{
		$sql = "select b.id as borrower_qty_id, a.member_id, a.first_name, a.last_name, a.email, a.address1, a.address2, a.phone1, a.phone2, b.type, b.refundable_qty, b.non_refundable_qty, b.return_qty, b.issue_date, b.return_date,b.status, c.title, c.image, c.coding,c.publisher from tbl_borrowers as a
                            join tbl_borrower_quantities as b on a.id = b.borrower_id
                            join tbl_publication_info as c on c.id = b.publication_id
                            where curdate() > b.return_date
                            and b.status = '0'";
		$query = $this->db->query($sql);
		return $query->result();
	}
        function get_active_borrower_list($publication_id){
            $sql = "select b.id as borrower_qty_id, a.member_id, a.first_name, a.last_name, a.email, a.address1, a.address2, a.phone1, a.phone2, b.type, b.refundable_qty, b.non_refundable_qty, b.return_qty, b.issue_date, b.return_date,b.status from tbl_borrowers as a
                            join tbl_borrower_quantities as b on a.id = b.borrower_id
                            where b.publication_id=?";
            $query = $this->db->query($sql,array($publication_id));
            return $query->result();
        }
}
?>